package org.jeecg.modules.workflow.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.jeecg.modules.workflow.vo.ProcessModelVO;

import java.util.List;

/**
 * 自定义activiti mapper接口
 * Create by Kalvin on 2020/4/22.
 */
public interface ActivitiMapper {

    @Update("update act_re_procdef set NAME_=#{name} where ID_=#{processDefinitionId}")
    void updateProcessDefinitionName(@Param("name") String name, @Param("processDefinitionId") String processDefinitionId);

    @Delete("delete from act_hi_actinst where PROC_INST_ID_ = #{processInstanceId} and ID_ > #{actId}")
    void deleteHisActivityInstanceByTaskId(@Param("processInstanceId")String processInstanceId, @Param("actId") String actId);

    @Select("select ID_ from act_hi_actinst where PROC_INST_ID_ = #{processInstanceId} and TASK_ID_ = #{taskId}")
    Integer hisActInstBy(@Param("processInstanceId")String processInstanceId, @Param("taskId") String taskId);

    @Delete("delete from act_hi_taskinst where ID_ = #{taskId}")
    void deleteHisTaskInstanceByTaskId(@Param("taskId") String taskId);

    @Delete("delete from act_hi_comment where TASK_ID_ = #{taskId}")
    void deleteComment(@Param("taskId") String taskId);

    @Delete("delete from act_ru_task where ID_ = #{taskId}")
    void deleteTask(@Param("taskId") String taskId);

    @Insert("insert into act_ru_task (ID_,REV_,EXECUTION_ID_,PROC_INST_ID_,PROC_DEF_ID_,NAME_,TASK_DEF_KEY_,ASSIGNEE_,PRIORITY_,CREATE_TIME_,SUSPENSION_STATE_,TENANT_ID_)\n" +
            "select ID_,'1',EXECUTION_ID_,PROC_INST_ID_,PROC_DEF_ID_,NAME_,TASK_DEF_KEY_,ASSIGNEE_,PRIORITY_,START_TIME_,1,TENANT_ID_\n" +
            "from act_hi_taskinst where ID_ = #{oldTaskId}")
    void insertTask(@Param("oldTaskId") String oldTaskId);

    @Update("update act_hi_taskinst set END_TIME_ = null where ID_ = #{taskId}")
    void setEndTimeNull(@Param("taskId") String taskId);

    @Select("SELECT " +
            "rm.ID_ as id," +
            "rm.NAME_ as name," +
            "rm.KEY_ as `key`," +
            "rm.VERSION_ as version," +
            "rm.DEPLOYMENT_ID_ as deploymentId," +
            "rm.CREATE_TIME_ as createTime," +
            "rm.LAST_UPDATE_TIME_ as lastUpdateTime," +
            "rp.NAME_ as processName," +
            "rp.ID_ as processDefinitionId," +
            "IF(rp.SUSPENSION_STATE_=1, false, IF(rp.SUSPENSION_STATE_ is null, null, true)) as processSuspended " +
            "FROM " +
            "act_re_model rm " +
            "LEFT JOIN act_re_procdef rp ON rm.DEPLOYMENT_ID_ = rp.DEPLOYMENT_ID_ ${ew.customSqlSegment}")
    List<ProcessModelVO> listProcessModels(@Param(Constants.WRAPPER) Wrapper wrapper, IPage page);

    @Update("update act_ru_execution set act_id_ = #{targetNodeId} where act_id_ = #{currentNodeId} and PROC_INST_ID_ = #{procInstId}")
    void updateRuExec(@Param("targetNodeId") String targetNodeId,@Param("currentNodeId") String currentNodeId,@Param("procInstId")String procInstId);
}
